<?php

namespace pmvc\template\smarty;

use pmvc\template\TemplateEngine;
use pmvc\ioc\InitializingObject;
use pmvc\log\Log;
use pmvc\util\ClassUtil;

/**
 * Smarty implementation of the {@link TemplateEngine}
 * interface.
 */
class SmartyTemplateEngine
	implements TemplateEngine,
	InitializingObject {

    public $compileDir			= './templates_c';
    public $configDir			= './configs';
    public $cacheDir			= './cache';
    public $templateDir			= './templates';
    public $useCache			= true;
    public $useSubDirs			= true;
    public $functions			= Array();
    public $blocks				= Array();
    public $compilerFunctions	= Array();
    public $modifiers			= Array();
    public $objects				= Array();
    public $outputFilters		= Array();
    public $postFilters			= Array();
    public $preFilters			= Array();

    /**
     * {@inheritdoc}
     */
	public function initialize() {
		
		// bring in smarty
		ClassUtil::requireFile("/smarty/Smarty.class.php", TRUE);
    	
		// create needed directories
		if (!file_exists($this->compileDir) 
			&& !mkdir($this->compileDir, 0755, TRUE)) {
			Log::error(__CLASS__, "Unable to create compile dir: {0}", $this->compileDir);
		}
		if (!file_exists($this->configDir) 
			&& !mkdir($this->configDir, 0755, TRUE)) {
			Log::error(__CLASS__, "Unable to create config dir: {0}", $this->configDir);
		}
		if (!file_exists($this->cacheDir) 
			&& !mkdir($this->cacheDir, 0755, TRUE)) {
			Log::error(__CLASS__, "Unable to create cache dir: {0}", $this->cacheDir);
		}
	}

    /**
     * {@inheritdoc}
     */
	public function getTemplate($name) {

        // create smarty
        $smarty = new \Smarty();
        
        // configure
        SmartyUtil::setUseSubDirs($smarty, $this->useSubDirs);
        SmartyUtil::setTemplateDir($smarty, $this->templateDir);
        SmartyUtil::setCompileDir($smarty, $this->compileDir);
        SmartyUtil::setConfigDir($smarty, $this->configDir);
        SmartyUtil::setCacheDir($smarty, $this->cacheDir);
        SmartyUtil::setCaching($smarty, strtolower($this->useCache)=="true");

        // add functions
        foreach ($this->functions as $fun) {
        	SmartyUtil::registerFunction($smarty,
        		$fun["name"], $$fun["callback"],
        		$fun["cacheable"], $fun["cache_attrs"]);
        }

        // add blocks
        foreach ($this->blocks as $block) {
        	SmartyUtil::registerBlock($smarty,
        		$fun["name"], $fun["callback"]);
        }

        // add compiler functions
        foreach ($this->compilerFunctions as $fun) {
        	SmartyUtil::registerCompilerFunction($smarty,
        		$fun["name"], $fun["callback"]);
        }

        // add modifiers
        foreach ($this->modifiers as $fun) {
        	SmartyUtil::registerModifier($smarty,
        		$fun["name"], $fun["callback"]);
        }

        // add objects
        foreach ($this->objects as $fun) {
        	SmartyUtil::registerObject($smarty,
        		$fun["name"], $fun["object"]);
        }

        // add output filters
        foreach ($this->outputFilters as $fun) {
        	SmartyUtil::registerOutputFilter($smarty, $fun);
        }

        // add post filters
        foreach ($this->postFilters as $fun) {
        	SmartyUtil::registerPostFilter($smarty, $fun);
        }

        // add pre filters
        foreach ($this->preFilters as $fun) {
        	SmartyUtil::registerPreFilter($smarty, $fun);
        }
 
        // return the template
		return new SmartyTemplate($smarty, $name);
	}
    
}

?>